<!DOCTYPE html>
<script src="../../../../../resources/testharness.js"></script>
<script src="../../../../../resources/testharnessreport.js"></script>
<script src="../../../../../resources/gesture-util.js"></script>
<div style="margin: 0px 0; width: 100px; height: 1000px; background-color: blue;"></div>
<script>

var EPSILON = 0.001;

var t = async_test("Scrolling with a scaled visual viewport");

// This test verifies scrolling of the page while pinch-zoomed in. We ensure
// that the scrolls target the correct (visual/layout) viewport and that
// bubbling between the two works correctly.
window.onload = t.step_func(async function() {
  await waitForCompositorCommit();
  await pinchBy(2, 400, 599, SPEED_INSTANT, GestureSourceType.TOUCHPAD_INPUT);
  await waitForCompositorCommit();
  assert_approx_equals(window.visualViewport.scale, 2.0, EPSILON);
  assert_approx_equals(window.visualViewport.offsetLeft, 200, EPSILON);
  assert_approx_equals(window.visualViewport.offsetTop, 300, EPSILON);
  // We're already within |EPSILON| of the desired offset, but we'll set it
  // here to avoid rounding issues later in the test.
  window.internals.setVisualViewportOffset(200, 300);
  await waitForCompositorCommit();

  // Scroll down, this will affect only the layout viewport.
  await smoothScroll(2, 400, 300, GestureSourceType.TOUCHPAD_INPUT, "down", SPEED_INSTANT);
  await waitForCompositorCommit();
  assert_equals(window.scrollY, 1);

  // Scroll right, this will affect only the visual viewport.
  await smoothScroll(2, 400, 300, GestureSourceType.TOUCHPAD_INPUT, "right", SPEED_INSTANT);
  await waitForCompositorCommit();
  assert_equals(window.visualViewport.offsetLeft, 201);

  // Move the visual viewport up by a pixel so that the next gesture will
  // bubble.
  window.internals.setVisualViewportOffset(201, 299);
  await waitForCompositorCommit();

  // Scroll back diagonally. This will affect both viewports by bubbling from
  // the visual to the layout.
  await smoothScroll(4, 400, 300, GestureSourceType.TOUCHPAD_INPUT, "downleft", SPEED_INSTANT);
  await waitForCompositorCommit();
  assert_equals(window.scrollY, 2);
  assert_equals(window.visualViewport.offsetTop, 300);
  assert_equals(window.visualViewport.offsetLeft, 199);

  t.done();
});

</script>
